import { request } from "../utils"
import { Toast } from "vant"

/*
  获取购物车
*/
const getCartProduct = (requestData, cb) => {
  request({
    url: "api/cart/GetCartProduct",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data.Shop[0] || []
      list = list.filter(item => {
        return item.Status === 0
      })
      cb && cb(list)
    } else {
      console.log("获取购物车失败...")
    }
  })
}

/*
  更新购物车数量
*/
const updateCartProduct = (requestData, cb) => {
  request({
    method: 'POST',
    url: "api/cart/PostUpdateCartItem",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("更新购物车数量失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  删除购物车
*/
const deleteCartProduct = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/cart/PostDeleteCartProduct",
    data: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("删除购物车失败...")
      Toast(res.data.Msg)
    }
  })
}

export { getCartProduct, updateCartProduct, deleteCartProduct }
